package com.tz.fruit.controller.backstage;

import com.tz.fruit.entity.Comment;
import com.tz.fruit.entity.Product;
import com.tz.fruit.service.IProductService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;

import java.util.List;

/**
 * Created by Administrator on 2017/7/25.
 */
@Controller(value="backCommentController")
@RequestMapping("/backstage/comment")
public class CommentController {
    @Autowired
    IProductService productService;

    /**
     * 根据商品Id查询所有评论
     */
    @RequestMapping("/list")
    public String list(Model model, Long pid){
        Product product = productService.findById(pid);
        List<Comment> comments = productService.findCommentByPid(pid);
        model.addAttribute("product",product);
        model.addAttribute("comments",comments);
        return "backstage/comment/list";
    }

    /**
     * 跳转到添加页面
     */
    @RequestMapping("/addPre")
    public String addPre(Model model,Long pid){
        //把对象放入request作用域
        model.addAttribute("pid",pid);
        return "/backstage/comment/add";
    }

    /**
     *  添加商品评论
     */
    @RequestMapping("/add")
//    public String add(Model model, @Validated/*(value = {Group1.class})*/@ModelAttribute Product product, BindingResult bindingResult){
    public String add(Model model, Comment comment){
//        if(bindingResult.hasErrors()){
//            List<ObjectError> errors = bindingResult.getAllErrors();
//            //把错误信息放入request作用域
//            model.addAttribute("errors",errors);
//            //查询所有推荐类型
//            RecState[] recStates = RecState.values();
//            //查询所有类别
//            List<Category> categories = productService.findAllCategory();
//            //把对象放入request作用域
//            model.addAttribute("recStates",recStates);
//            model.addAttribute("categories",categories);
//            return "backstage/product/add";
//        }
        productService.save(comment);
        return "redirect:list?pid="+comment.getProduct().getPid();
    }

    /**
     * 删除商品评论
     */
    @RequestMapping("/delete")
    public String delete(Long[] commentId,Long pid){
        for(int i=0;i<commentId.length;i++){
            productService.delByCommmentId(commentId[i]);
        }
        return "redirect:list?pid="+pid;
    }

    /**
     * 跳转到编辑页面
     */
    @RequestMapping("/editPre")
    public String editPre(Model model,Long pid,Long commentId){
        //根据商品id查询所有简介
        Comment comment = productService.findByCommmentId(commentId);
        //把对象放入request作用域
        model.addAttribute("pid",pid);
        model.addAttribute("comment",comment);
        return "/backstage/comment/edit";
    }

    /**
     * 更新商品简介
     */
    @RequestMapping("/edit")
    public String edit(Comment comment){
        productService.update(comment);
        return "redirect:list?pid="+comment.getProduct().getPid();
    }
}
